package {
	import flash.display.Sprite;
	import flash.filesystem.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.display.DisplayObjectContainer;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.FileFilter;
	import flash.display.NativeWindow;
	import flash.display.Stage;
	import com.bit101.components.*;
	public class quickM extends Sprite {
		private var _win:NativeWindow; 
		 
		var fileToOpen:File = new File();
		var txtFilter:FileFilter = new FileFilter("MP3", "*.mp3;*.MP3");

		var bytearray:ByteArray=new ByteArray();
		var loader:Loader=new Loader();

		var song:SoundChannel;
		var sound:Sound;

		var _close:PushButton;
		var playBtn:PushButton;//play music button
		var pushBtn:PushButton;//change local music button
		var vslider1:VSlider;//volume slider
		var vslider2:VSlider;
		var vslider3:VSlider;
		var vslider4:VSlider;
		var label1:Label;//volume label
		var label2:Label;
		var label3:Label;
		var label4:Label;
		var labeltime:Label;
		var musicurl:Text;//muisc url
		public function quickM() {
			stage.addEventListener(MouseEvent.MOUSE_DOWN,eventHandler);    
			_win = stage.nativeWindow;
			_win.title = "quickMusic-敏捷型播放器";
			stage.showDefaultContextMenu = false;
			
			var spectrum:Spectrum = new Spectrum(100, 50);//阶梯状声谱
			addChild(spectrum);
			spectrum.y = 120;
			//simpleComponents-------------------------------------------------------
			_close = new PushButton(this,180,20,"close");      
            _close.addEventListener(MouseEvent.CLICK,eventHandler);    
			
			playBtn = new PushButton(this, 0, 50);
			pushBtn=new PushButton(this,0,30);
			vslider1=new VSlider(this,110,30);
			vslider2=new VSlider(this,130,30);
			vslider3=new VSlider(this,150,30);
			vslider4 = new VSlider(this, 170, 30);
			label1=new Label(this,110,130,"V");
			label2=new Label(this,130,130,"P");
			label3=new Label(this,150,130,"L");
			label4=new Label(this,170,130,"R");
			labeltime = new Label(this, 0, 70, "time");
			musicurl = new Text(this, 0, 0, "you can enter internet music!");
		
			playBtn.label="Play";
			playBtn.addEventListener(MouseEvent.CLICK,onClickBtnHandler);
			
			pushBtn.label="local music";
			pushBtn.addEventListener(MouseEvent.CLICK,onClickBtnHandler);
			
			vslider1.value=9;
			vslider2.value=56;
			vslider3.value=21;
			vslider4.value=49;
			vslider1.addEventListener(Event.CHANGE,changeSliderHandler);
			vslider2.addEventListener(Event.CHANGE,changeSliderHandler);
			vslider3.addEventListener(Event.CHANGE,changeSliderHandler);
			vslider4.addEventListener(Event.CHANGE,changeSliderHandler);

			musicurl.height=20;
			musicurl.width=300;
			//---------------------------------------------------------------------
		}
		public function eventHandler(event:MouseEvent):void{    
            switch(event.target){    
                case stage:    
                    _win.startMove();    
                break;    
                case _close:    
                    _win.close();    
                break;    
            }    
        }    
           
		private function changeSliderHandler(e:Event):void{
			if(!song) return;
			var transform:SoundTransform = song.soundTransform;
			switch(e.target){
				case vslider1:
					transform.volume=e.target.value/100;
					song.soundTransform=transform;
					trace(song.soundTransform.volume);
				break;
				case vslider2:
		
				break;
				case vslider3:
			
				break;
				case vslider4:
			
				break;
			}
		}

		private function onClickBtnHandler(e:MouseEvent):void{
		//loader.loadBytes(bytearray);
			if(e.currentTarget==playBtn) return;
			try 
			{
				fileToOpen.browseForOpen("Open", [txtFilter]);
				fileToOpen.addEventListener(Event.SELECT, fileSelected);
			}
			catch (error:Error)
			{
				trace("Failed:", error.message);
			}
		}

		private function onEnterFrameHandler(e:Event):void{
			labeltime.text="Time "+calcLength(song.position) + " / " + calcLength(sound.length);
		}
		private function calcLength(p:Number):String
		{
			var minutes:uint = ((p / 1000) / 60) % 60;
			var seconds:uint = (p / 1000) - (minutes * 60);
			var str_seconds:String = seconds.toString();
			if(str_seconds.length == 1)
			{
				str_seconds = "0" + str_seconds;
			}
			return minutes.toString() + ":" + str_seconds;
		}
		private function fileSelected(event:Event):void 
		{
			var request:URLRequest = new URLRequest(fileToOpen.nativePath);
			musicurl.text=fileToOpen.nativePath;

			if (sound) song.stop();
			sound = new Sound();
			sound.addEventListener(Event.COMPLETE, completeHandler);

			sound.load(request);
			song = sound.play();
			
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		private function completeHandler(e:Event):void{
	
		}
	}
}